package net.lym.es.doc;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

/**
 * ES测试 : 文档数据查询
 */
public class ESTest_Doc_Get {
    public static void main(String[] args) throws Exception {
        // 创建 ES 客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost", 9200, "http"))
        );

        // 查询文档 - 请求对象
        GetRequest request = new GetRequest();
        // 设置索引及唯一性标识
        request.index("user").id("1001");
        // 客户端发送请求，获取响应对象
        GetResponse response = esClient.get(request, RequestOptions.DEFAULT);
        // 打印结果信息
        System.out.println("_index:" + response.getIndex());            // 索引
        System.out.println("_type:" + response.getType());              // 类型
        System.out.println("_id:" + response.getId());                  // 唯一性标识
        System.out.println("source:" + response.getSourceAsString());   // 数据

        // 关闭 ES 客户端
        esClient.close();
    }
}
